package press.xiang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import lombok.AllArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import press.xiang.entity.Store;
import press.xiang.service.IStoreService;

import java.util.*;

/**
 * @author xiang
 * @version 1.0
 * @description: TODO
 * @date 2022/4/21 6:43 下午
 */
@RestController
@RequestMapping("store")
@AllArgsConstructor
public class StoreController {

    private final IStoreService storeService;

    @GetMapping("query")
    public R<List<Store>> query(){
        return R.ok(new Store().selectAll());
    }

    @GetMapping("query-by-type")
    public R<List<Store>> queryByStore(@RequestBody Store store){
        LambdaQueryWrapper<Store> eq = Wrappers.<Store>query().lambda()
                .eq(Store::getType,store.getType());
        List<Store> list = storeService.list(eq);
        return R.ok(list);
    }

    @PostMapping("save")
    public R save(@RequestBody Store store){
        if (StringUtils.isEmpty(store.getId())){
            store.setId(UUID.randomUUID().toString());
        }
        return R.ok(store.insert());
    }

    @PostMapping("update")
    public R update(@RequestBody Store store){
        store.updateById();
        return R.ok("success");
    }

    @PostMapping("delete")
    public R delete(@RequestBody Store store){
        store.deleteById();
        return R.ok("success");
    }

    /**
     * @description: 生成随机个仓库
     * @param count 随机个数的仓库
     * @param type 类型仓库
     * @return: com.baomidou.mybatisplus.extension.api.R
     * @author xiang
     * @date: 2022/4/22 10:58 上午
     */
    @PostMapping("create-random-store")
    public R createRandomStore(int count,String type){
        Set<String> set = new HashSet<>();
        Random r = new Random();
        while (set.size()<count){
            int d = r.nextInt(1000);
            set.add(type+String.valueOf(d));
        }

        for (String s : set) {
            Store store = new Store();
            store.setName(s);
            store.setType(type);
            String l = String.valueOf(r.nextInt(100));
            String t = String.valueOf(r.nextInt(100));
            store.setLongAndLat(l+","+t);
            store.setId(UUID.randomUUID().toString());
            store.insert();
        }
        return R.ok("success");
    }


    @PostMapping("truncate-store")
    public R truncateStore(){
        storeService.truncateStore();
        return R.ok("success");
    }


}
